<template>
  <div class="bg-white personal_info_update">
    <div class="handle_type">{{ handleType }}</div>
    <div class="form-conatiner">
      <ElForm :model="personalInfo" label-width="auto" style="max-width: 600px">
        <ElFormItem label="昵称">
          <ElInput v-model="personalInfo.name" />
        </ElFormItem>
        <ElFormItem label="头像">
          <ElUpload action="#" list-type="picture-card" :http-request="handleUpload" >
            <ElIcon><Plus /></ElIcon>
            <template #file="{ file }">
              <div>
                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <ElIcon><zoom-in /></ElIcon>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleDownload(file)"
                  >
                    <ElIcon><Download /></ElIcon>
                  </span>
                  <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <ElIcon><Delete /></ElIcon>
                  </span>
                </span>
              </div>
            </template>
          </ElUpload>
        </ElFormItem>
        <ElFormItem label="性别">
          <ElRadioGroup v-model="personalInfo.sex">
            <ElRadio v-for="item in sexType" :key="item.value" :value="item.value">{{
              item.label
            }}</ElRadio>
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem label="简介">
          <ElInput v-model="personalInfo.introduction" type="textarea" />
        </ElFormItem>
        <ElFormItem label="手机">
          <ElInput v-model="personalInfo.phoneNumber" type="phone" />
        </ElFormItem>
        <ElFormItem label="地址">
          <ElInput v-model="personalInfo.address"  />
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" @click="handleSubmit" class="submit_btn">提交</ElButton>
        </ElFormItem>
      </ElForm>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import type { UploadFile } from 'element-plus'
import {useRouter} from 'vue-router'
import { useUserStore } from '@/stores/user'


const router = useRouter()
const userStore = useUserStore()

const handleType = ref('修改信息')

const personalInfo = reactive({
  id:'10035',
  name: '',
  avatar: '',
  sex: '',
  introduction: '',
  phoneNumber: '',
  address: '',
})

const sexType = [
  {
    label: '男',
    value: 0,
  },
  {
    label: '女',
    value: 1,
  },
]

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)

const handleUpload = async (options: UploadEntity) => {
  await service.common.upload(options)
}
const handleRemove = (file: UploadFile) => {
  console.log(file)
}

const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}

const handleDownload = (file: UploadFile) => {
  console.log(file)
}
const handleSubmit = async () => {
  await service.user.updateUserInfo(personalInfo)
  // 更新userStore
  userStore.userInfo = {...userStore.userInfo,...personalInfo,uid:personalInfo.id }
  router.push('/main/personalHomepage')
}

const getUserData=async(id:string|number)=>{
  // id
  const result = await service.user.getUserInfo()
  console.log('result=>',result)
}

onMounted(()=>{
  getUserData(personalInfo.id)
})
</script>

<style lang="scss" scoped>
.personal_info_update {
  padding: 20px 0;

  .handle_type {
    padding: 0 20px 10px;
    margin-bottom: 10px;
    border-bottom: 2px solid rgba(0, 0, 0, 10%);
  }

  .form-conatiner {
    width: 65%;
    margin: 0 auto;

    .submit_btn {
      width: 300px;
      margin: 0 auto;
    }
  }
}
</style>
